package it.traveldream.customer;

import it.traveldreamClient.beans.ComponentiMgrBeanInterface;
import it.traveldreamClient.beans.UserMgr;
import it.traveldreamClient.beans.dto.ComponenteDTO;
import it.traveldreamClient.beans.dto.ProdottoAcquistabileDTO;
import it.traveldreamClient.beans.dto.UserDTO;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name="acquisti")
@ViewScoped
public class RichiesteAcquistiBean {

	@EJB
	private UserMgr usr;
	@EJB
	private ComponentiMgrBeanInterface comp;
	
	private List<String> selectedItems,acquisti;
	private List<ComponenteDTO> regaliEffettuati, regaliRicevuti;
	private UserDTO userDTO;
	
	public RichiesteAcquistiBean () {
		selectedItems=new LinkedList<String>();
		regaliEffettuati= new LinkedList<ComponenteDTO>();
		regaliRicevuti= new LinkedList<ComponenteDTO>();
	}
	@PostConstruct
	private void init(){
		userDTO=usr.getUserDTO();
		acquisti=new LinkedList<String>();
		for(ComponenteDTO cmp : userDTO.getGiftList()) {
			if(cmp.isRegalo()){
				regaliRicevuti.add(cmp);
			}
		}
		List<ProdottoAcquistabileDTO> escapeFromConcurrencyExceptionList=new LinkedList<ProdottoAcquistabileDTO>(userDTO.getAcquisti());
		for(ProdottoAcquistabileDTO p : escapeFromConcurrencyExceptionList) {
			if(p instanceof ComponenteDTO) {
				ComponenteDTO cmp=(ComponenteDTO) p;
				if(cmp.isRegalo()){
					regaliEffettuati.add(cmp);
					userDTO.getAcquisti().remove(cmp);
				}
				
			}
			acquisti.add(p.toString());
		}
		
		
		
		
		
		
	}

	public List<String> getSelectedItems() {
		return selectedItems;
	}

	public void setSelectedItems(List<String> selectedItems) {
		this.selectedItems = selectedItems;
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
	
	public void remove(ActionEvent actionEvent){
		for(ProdottoAcquistabileDTO p : userDTO.getAcquisti()){
			if(selectedItems.contains(p.toString())){
				userDTO.getAcquisti().remove(p);
				acquisti.remove(p.toString());
			}
		}
		usr.updateAcquisti(userDTO);
	}
	
	public List<ComponenteDTO> getRegaliEffettuati() {
		return regaliEffettuati;
	}
	
	public List<ComponenteDTO> getRegaliRicevuti() {
		return regaliRicevuti;
	}
	public void setRegaliRicevuti(List<ComponenteDTO> regaliRicevuti) {
		this.regaliRicevuti = regaliRicevuti;
	}
	public void setRegaliEffettuati(List<ComponenteDTO> regaliEffettuati) {
		this.regaliEffettuati = regaliEffettuati;
	}
	public List<String> getAcquisti() {
		return acquisti;
	}
	public void setAcquisti(List<String> acquisti) {
		this.acquisti = acquisti;
	}
	
	
	
	
	
	
}
